<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('修改二手车')" />
    <th:block th:include="include :: select2-css"/>
</head>
<style>
    .divImg {
        float: left;
        margin: 2px;
        padding: 2px;
        height: 108px;
        width: 100px;
        border: 1px solid #dcdcdc;
        cursor: pointer;
    }
</style>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-used-edit" th:object="${carUsed}">
            <input name="id" th:field="*{id}" type="hidden">
            <div class="form-group">    
                <label class="col-sm-3 control-label">二手车名称：</label>
                <div class="col-sm-8">
                    <input name="name" th:field="*{name}" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">价格：</label>
                <div class="col-sm-8">
                    <input name="price" th:field="*{price}" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">品牌：</label>

                <div class="col-sm-8">
                <select id="brandId" name="brandId" class="form-control m-b"  >
                    <option th:each="dict : ${brand}" th:text="${dict.name}" th:value="${dict.id}" th:field="*{brandId}"></option>
                </select>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">车型：</label>
                <div class="col-sm-8">
                    <select id="carType" name="carType" class="form-control m-b"  >
                        <option>请先选择品牌</option>
                    </select>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">门店：</label>
                <div class="col-sm-8">
                    <select name="dealerId" class="form-control m-b"  >
                        <option th:each="dict : ${dealerList}" th:text="${dict.name}" th:value="${dict.id}" th:field="*{carType}"></option>
                    </select>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">主图(578*277)：</label>
                <div class="col-sm-8">
                    <button type="button" class="layui-btn" id="upload_main">上传图片</button>
                    <div class="layui-upload-list">
                        <input type="hidden" id="mainUrl" th:value="*{mainUrl}" name="mainUrl">
                        <p id="show_main">
                            <img th:src="*{mainUrl}" style="width:80px;height:80px;">
                        </p>
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">详情图(750*388)：</label>
                <div class="col-sm-8">
                    <button type="button" class="layui-btn" id="upload_info">上传图片</button>
                    <div class="layui-upload-list">
                        <input type="hidden" id="infoUrl" th:value="*{infoImg}" name="infoImg">
                        <p id="show_info">
                            <img th:src="*{infoImg}" style="width:80px;height:80px;">
                        </p>
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-2 control-label"><span style="color: red">*</span>轮播图(750*388)</label>
                <div class="col-sm-8">
                    <div style="text-align: left;">
                        <button type="button" class="layui-btn" id="upload_car_img0" >上传图片</button>
                    </div>
                    <div class="layui-upload-list">
                        <div id="img-url0" style="padding: 5px;"></div>
                    </div>
                </div>
            </div>

            <div class="form-group">
                <label class="col-sm-3 control-label">状态：</label>
                <div class="col-sm-8">
                    <div class="radio-box" th:each="dict : ${@dict.getType('used_status')}">
                        <input type="radio" th:id="${'status_' + dict.dictCode}" name="status"
                               th:value="${dict.dictValue}" th:checked="${dict.default}" th:field="*{status}">
                        <label th:for="${'status_' + dict.dictCode}" th:text="${dict.dictLabel}"></label>
                    </div>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">排序：</label>
                <div class="col-sm-8">
                    <input name="sort" th:field="*{sort}" class="form-control" type="text">
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: select2-js" />
    <th:block th:include="include :: sortable-js"/>
    <script type="text/javascript" th:inline="javascript">



        $(document).ready(function () {
            updateCarType([[${carUsed.carType}]])
        })

        $("#brandId").on("change",function(){
            updateCarType(null);
        });

        function updateCarType(carType) {
            var brandId = $("#brandId").val();
            //执行 Ajax
            $.ajax({
                url : prefix+"/type/"+brandId+"/all",
                type : "get",
                success : function(result) {
                    if(result.code==0){
                        var ss = "";
                        if(carType!=null){
                            $.each(result.data,function (index,value) {
                                if(carType==value.id){
                                    ss+="<option value='"+value.id+"' selected='selected'>"+value.name+"</option>"
                                }else {
                                    ss+="<option value='"+value.id+"'>"+value.name+"</option>"
                                }
                            })
                        }else{
                            $.each(result.data,function (index,value) {
                                ss+="<option value='"+value.id+"'>"+value.name+"</option>"
                            })
                        }
                        $("#carType").html(ss);
                    }
                },
                error:function () {
                    layer.msg('失败',{'icon':2});
                }
            });
        }


        var imgId0=0;
        if([[${carImg0}]]!=undefined && [[${carImg0}]]!=[]) {
            var carImg0 =[[${carImg0}]];
            for (o in carImg0) {
                imgId0++;
                $("#img-url0").append("<div id='imgId0" + imgId0 + "' class='divImg'>" +
                    "<img src='" + carImg0[o].url + "' style='width:100px;height: 80px;margin-bottom: 1px;'><br>" +
                    "<input type='hidden' name='carImg0' value='" + carImg0[o].url + "'>" +
                    "<i style='float: right;padding-top: 4px;' class='fa fa-close' onclick=\"delImg('imgId0" + imgId0 + "')\"></i></div>");
                // $("#logo").val(res.url);
            }
        }

        layui.use('upload', function () {
            var $ = layui.jquery
                , upload = layui.upload;



            var container0 = document.getElementById("img-url0");
            var sort0 = Sortable.create(container0);
            // var sortDetail = Sortable.create(containerDetail);

            //普通图片上传
            upload.render({
                elem: '#upload_car_img0'
                , url: ctx + '/common/upload' //改成您自己的上传接口
                , multiple: true
                , before: function (obj) {
                    //预读本地文件示例，不支持ie8
                    obj.preview(function (index, file, result) {
                        // $('#img-url').append('<img src="'+ result +'" alt="'+ file.name +'" class="layui-upload-img">')

                        var name = file.name.split(".")[0];
                        console.log(name)
                        $("#img-url0").append("<div id='div" + name + "'><div  class='divImg'>" +
                            "<img  id='src" + name + "' src='' style='width:100px;height: 80px;margin-bottom: 1px;'><br>" +

                            "<i style='float: right;padding-top: 4px;' class='fa fa-close' onclick=\"delImg('div" + name + "')\"></i></div></div>");
                        // $("#logo").val(res.url);
                    });
                }
                , done: function (res) {
                    //如果上传失败
                    if (res.code > 0) {
                        return layer.msg('上传失败');
                    }
                    imgId0++;
                    $("#div" + res.name).replaceWith("<div id='imgId0" + imgId0 + "' class='divImg'>" +
                        "<img  src='" + res.url + "' style='width:100px;height: 80px;margin-bottom: 1px;'><br>" +
                        "<input type='hidden' name='carImg0' value='" + res.url + "'>" +
                        "<i style='float: right;padding-top: 4px;' class='fa fa-close' onclick=\"delImg('imgId0" + imgId0 + "')\"></i></div>");

                    sort0.destroy();
                    sort0 = Sortable.create(container0);


                }
            });


            layui.use('upload', function () {
                var $ = layui.jquery
                    , upload = layui.upload;

                //普通图片上传
                upload.render({
                    elem: '#upload_main'
                    , url: ctx+'/common/upload' //改成您自己的上传接口
                    , done: function (res) {
                        //如果上传失败
                        if (res.code > 0) {
                            return layer.msg('上传失败');
                        }
                        //上传成功
                        $("#show_main").html("<img src='" + res.url + "' style='width:80px;height:80px;'>");
                        $("#mainUrl").val(res.url);
                    }
                });
                //普通图片上传
                upload.render({
                    elem: '#upload_info'
                    , url: ctx+'/common/upload' //改成您自己的上传接口
                    , done: function (res) {
                        //如果上传失败
                        if (res.code > 0) {
                            return layer.msg('上传失败');
                        }
                        //上传成功
                        $("#show_info").html("<img src='" + res.url + "' style='width:80px;height:80px;'>");
                        $("#infoUrl").val(res.url);
                    }
                });

            })

        })

        function delImg(id) {
            $("#" + id).remove();
        }
        var prefix = ctx + "system/used";
        $("#form-used-edit").validate({
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
                $.operate.save(prefix + "/edit", $('#form-used-edit').serialize());
            }
        }
    </script>
</body>
</html>